package com.itheima.sh.f_ajax_json_test_06;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BaseController {
    //1.静态方法封装前端数据到pojo对象里
    //定义泛型T接收
    public static <T> T jsonToPojo(HttpServletRequest request,Class<T> clazz) throws IOException {
        //将前端传来信息封装到pojo的大Class里
        Object o = JSON.parseObject(request.getInputStream(), clazz);
        return (T)o;
    }

    //2.静态方法响应数据到前端
    public static void printJsonToBrowser(Result result, HttpServletResponse response) throws IOException {
        //将后端数据封装到result对象里，转成json格式传回前端
        String s = JSON.toJSONString(result);
        response.getWriter().print(s);

    }

}
